<template>
  <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
    <el-form-item prop="phone">
      <el-input 
        v-model="loginForm.phone"
        placeholder="请输入手机号"
      >
        <template #prefix>
          <el-icon><Iphone /></el-icon>
        </template>
      </el-input>
    </el-form-item>
    
    <el-form-item prop="code">
      <div class="code-input">
        <el-input 
          v-model="loginForm.code"
          placeholder="请输入验证码"
          class="code-field"
        >
          <template #suffix>
            <div class="send-code-btn-wrapper">
              <el-button 
                :disabled="isCountDown"
                @click="handleSendCode"
                class="send-code-btn"
                :class="{ counting: isCountDown }"
                type="primary"
                link
              >
                {{ countDownText }}
              </el-button>
            </div>
          </template>
        </el-input>
      </div>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" :loading="loading" @click="handleLogin" style="width: 100%">
        登录
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { Iphone } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { sendSmsCode, smsLogin } from '@/api/login'
import { useUserStore } from '@/store/modules/user'
import { useRouter } from 'vue-router'

const loading = ref(false)
const loginFormRef = ref()
const isCountDown = ref(false)
const countDown = ref(60)
const countDownText = ref('获取验证码')

const loginForm = reactive({
  phone: '',
  code: ''
})

const rules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}

const handleSendCode = async () => {
  try {
    await loginFormRef.value.validateField('phone')
    await sendSmsCode(loginForm.phone)
    ElMessage.success('验证码发送成功')
    startCountDown()
  } catch (error) {
    if (!error.message) return
    ElMessage.error(error.message || '验证码发送失败')
  }
}

const startCountDown = () => {
  isCountDown.value = true
  const timer = setInterval(() => {
    countDown.value--
    countDownText.value = `${countDown.value}秒后重新获取`
    if (countDown.value <= 0) {
      clearInterval(timer)
      isCountDown.value = false
      countDown.value = 60
      countDownText.value = '获取验证码'
    }
  }, 1000)
}

const router = useRouter()
const userStore = useUserStore()

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        await userStore.loginBySms(loginForm)
        ElMessage.success('登录成功')
        router.push('/')
      } catch (error) {
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.el-form {
  margin-top: 20px;
}

:deep(.el-input__wrapper) {
  background: #f5f7fa;
  border-radius: 4px;
  box-shadow: none;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #1890ff;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1890ff;
}

.code-input {
  position: relative;
  width: 100%;
}

.code-field {
  width: 100%;
}

.send-code-btn-wrapper {
  height: 40px;
  display: flex;
  align-items: center;
  padding: 0 0 0 8px;
  border-left: 1px solid #e4e7ed;
}

:deep(.send-code-btn) {
  height: 38px;
  padding: 0 4px;
  font-size: 14px;
  color: #1890ff;
  transition: all 0.3s;
  white-space: nowrap;
  display: flex;
  align-items: center;
}

:deep(.send-code-btn:not(:disabled):hover) {
  color: #40a9ff;
  background: transparent;
}

:deep(.send-code-btn.counting) {
  color: #999;
  cursor: not-allowed;
}

:deep(.send-code-btn:disabled) {
  background: transparent;
  border-color: transparent;
}

.el-button--primary {
  height: 40px;
  font-size: 16px;
  border-radius: 4px;
  margin-top: 8px;
}

/* 修改输入框内边距相关样式 */
:deep(.el-input__wrapper) {
  padding: 0 11px;
  height: 40px;
}

:deep(.el-input__inner) {
  height: 38px;
  line-height: 38px;
}

/* 修改前缀图标的样式 */
:deep(.el-input__prefix) {
  margin-right: 8px;
}

:deep(.el-input__prefix-inner .el-icon) {
  color: #999;
  font-size: 16px;
}

/* 修改后缀区域样式 */
:deep(.el-input__suffix) {
  padding: 0;
  height: 100%;
}

/* 确保验证码按钮文字垂直居中 */
:deep(.send-code-btn .el-button__text) {
  line-height: normal;
}

/* 确保所有输入框高度一致 */
.el-input {
  --el-input-height: 40px;
}
</style> 